package com.equipment;

import java.util.NoSuchElementException;

import com.util.EncodingUtil;

/**
 * 参数设置响应幀B9
 * @version 1.18
 * @author 刘亚东
 *
 */
public class SetParameterResponse {

	private String STX = "55AA"; // 帧开始标志
	private String RSCTL = "00"; // 串口帧序列号
	private String Length = ""; // 长度
	private String code = "DF"; // 代码
	private String broadening = "D9"; // 扩展代码
	private String BCC = ""; // 异或校验值

	private String Status = "";// 0表示成功,非0表示失败
	private String other = "0000000000000000000000000000000000000000";// 保留字节(长度16)

	public SetParameterResponse analysis(String context)throws NoSuchElementException  {
		SetParameterResponse sr = null;
		try {
			//55AA050012DFD9000000000000000000000000000000000011
			if (!context.substring(12,14).toUpperCase().equals("D9")) {
				throw new NoSuchElementException("初始化失败");
			}
			if (!context.substring(14, 16).equals("00")) {
				throw new NoSuchElementException("初始化失败");
			}
			// 截取到bcc校验值 需要判断校验是否相等
			BCC = context.substring(context.length() - 2).toUpperCase();
			// 截取到发送的数据(总长度-2个字节计算到数据的最后位置)并转换下FE和FF
			String str = context.substring(4, context.length() - 2);
			// 得到比对校验
			str = EncodingUtil.yihuo(EncodingUtil.change(str)).toUpperCase();
			// 判断校验是否一致
			if (!str.equals(BCC)) {
				throw new NoSuchElementException("初始化失败");
			}
			// 如果没问题就new 对象 把解析的值放入对象
			sr = new SetParameterResponse();
			// 解析串口帧序列号
			RSCTL = context.substring(4, 6);
			sr.setRSCTL(RSCTL);
			// 解析长度
			Length = context.substring(6, 10);
			sr.setLength(Length);
			// 解析代码类型
			code = context.substring(10,12);
			sr.setCode(code);
			// 解析拓展子代码
			broadening = context.substring(12, 14);
			sr.setBroadening(broadening);
			// 解析更新状态
			Status = context.substring(14, 16);
			sr.setStatus(Status);
			// 解析保留字节
			other = context.substring(16, 48);
			sr.setOther(other);
			// 解析异或校验值
			sr.setBCC(BCC);
		} catch (Exception e) {
			e.printStackTrace();
			throw new NoSuchElementException("初始化失败");
		}
		return sr;
	}

	public String getSTX() {
		return STX;
	}

	public void setSTX(String sTX) {
		STX = sTX;
	}

	public String getRSCTL() {
		return RSCTL;
	}

	public void setRSCTL(String rSCTL) {
		RSCTL = rSCTL;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getBroadening() {
		return broadening;
	}

	public void setBroadening(String broadening) {
		this.broadening = broadening;
	}

	public String getBCC() {
		return BCC;
	}

	public void setBCC(String bCC) {
		BCC = bCC;
	}

	public String getStatus() {
		return Status;
	}

	public void setStatus(String status) {
		Status = status;
	}

	public String getOther() {
		return other;
	}

	public void setOther(String other) {
		this.other = other;
	}

	public String getLength() {
		return Length;
	}

	public void setLength(String length) {
		Length = length;
	}

}
